package com.sczmgk.frame.http;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.sczmgk.hotline.utils.BitmapUtils;


public class HttpBitmapRequest extends HttpDownloadRequest {

    public static final int DEFAULT_MEASURE_HEIGHT = 960;
    public static final int DEFAULT_MEASURE_WIDTH = 960;

    private final String TAG = "HttpBitmapRequest";

    private boolean isNeedCache = true;
    private int mMaxMeasureHeight = -1;
    private int mMaxMeasureWidth = -1;
    private BitmapFactory.Options mOptions = null;

    /**
     * 100*100
     */
    public static final String STYLE_NAME_IMAGE_SMALL = "small";
    /**
     * 100*100
     */
    public static final String STYLE_NAME_VIDEO_IMAGE_SMALL = "vset_small";

    public static final String STYLE_NAME_IMAGE_SMALL_160 = "small160";

    public static final String STYLE_NAME_IMAGE_SMALL_360 = "small360";

    public static final String STYLE_NAME_IMAGE_SMALL_480 = "small480";

    public static final String STYLE_NAME_IMAGE_SMALL_600 = "small600";

    public static final String STYLE_NAME_IMAGE_SMALL_810 = "small810";

    public static final String STYLE_NAME_CLIP_IMAGE_SMALL_80 = "smallj80";

    public static final String STYLE_NAME_CLIP_IMAGE_SMALL_120 = "smallj120";

    public static final String STYLE_NAME_CLIP_IMAGE_SMALL_160 = "smallj160";

    public static final String STYLE_NAME_CLIP_IMAGE_SMALL_200 = "smallj200";

    public static final String STYLE_NAME_CLIP_IMAGE_SMALL_270 = "smallj270";


    public HttpBitmapRequest(Context context, String downloadUrl, String requestMethod) {
        super(context, downloadUrl, null, null);
        setRequestMethod(requestMethod);
    }

    public HttpBitmapRequest(Context context, String downloadUrl, String savePath, TokenCallback tokenCallback) {
        super(context, downloadUrl, savePath, tokenCallback);
    }

    public HttpBitmapRequest(Context context, String downloadUrl, String styleName, String savePath, TokenCallback tokenCallback) {
        super(context, downloadUrl, savePath, styleName, tokenCallback);
    }

    public void setOptions(BitmapFactory.Options options) {
        this.mOptions = options;
    }

    @Override
    public HttpResponse onPreExecute() throws Exception {
        boolean isNeedSave = false;
        String filePath = (String) DownloadFileSaveUtil.getUsableCachePath(mContext, mCacheXmlUrl);
        if (TextUtils.isEmpty(filePath)) {
            filePath = (String) DownloadFileSaveUtil.getUsableCachePath(mContext, getFileName());
            isNeedSave = true;
        }
        try {
            if (!TextUtils.isEmpty(filePath)) {
                Bitmap bm = null;
                if (mMaxMeasureHeight > 0 && mMaxMeasureWidth > 0) {
                    bm = BitmapUtils.getBitmap(filePath, mMaxMeasureWidth, mMaxMeasureHeight, mOptions);
                } else {
                    bm = BitmapUtils.getBitmap(filePath, DEFAULT_MEASURE_WIDTH, DEFAULT_MEASURE_HEIGHT, mOptions);
                }
                if (bm != null && !bm.isRecycled()) {
                    if (isNeedCache) {
//						BitmapManager.getInstance().cacheNetworkBitmap(getBitmapCacheUrl(), bm);
                    }
                    if (isNeedSave && !Tools.isEmpty(getSavePath())) {

                        String imagePath = saveImageFile(FileUtil.getExtensionName(filePath), bm,
                                DownloadFileSaveUtil.DownloadFileSave.FIXED_LENGTH, DownloadFileSaveUtil.DownloadFileSave.FIXED_LENGTH);

                        if (!Tools.isEmpty(imagePath)) {
                            filePath = imagePath;
                        }
                    }
                    return new HttpBitmapResponse(HttpResponse.STATE_SUCCESS, this, bm, filePath);
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (TextUtils.isEmpty(getFileName())) {
            return new HttpBitmapResponse(HttpResponse.STATE_FAILED, this);
        }
        return super.onPreExecute();
    }

    @Override
    public HttpResponse executeContent(byte[] content, String extension, long fileLength) {
        Bitmap downloadBitmap = BitmapFactory.decodeByteArray(content, 0, content.length);
        if (mMaxMeasureHeight > 0 && mMaxMeasureWidth > 0) {
            downloadBitmap = BitmapUtils.getBitmap(content, mMaxMeasureWidth, mMaxMeasureHeight, mOptions);
        } else {
            downloadBitmap = BitmapUtils.getBitmap(content, DEFAULT_MEASURE_WIDTH, DEFAULT_MEASURE_HEIGHT, mOptions);
        }
        return excuteBitmap(downloadBitmap, null, extension);
    }


    @Override
    protected HttpResponse executeContent(String filePath, String extension, long fileLength) {
        Bitmap downloadBitmap = null;
        Bitmap saveBitmap = null;
        String imagePath = null;
        if (mMaxMeasureHeight > 0 && mMaxMeasureWidth > 0) {
            downloadBitmap = BitmapUtils.getBitmap(filePath, mMaxMeasureWidth, mMaxMeasureHeight, mOptions);
            if (!Tools.isEmpty(getSavePath())) {
                saveBitmap = BitmapUtils.getBitmap(filePath, DEFAULT_MEASURE_WIDTH, DEFAULT_MEASURE_HEIGHT, mOptions);
            }
        } else {
            downloadBitmap = BitmapUtils.getBitmap(filePath, DEFAULT_MEASURE_WIDTH, DEFAULT_MEASURE_HEIGHT, mOptions);
            if (!Tools.isEmpty(getSavePath())) {
                saveBitmap = downloadBitmap;
            }
        }

        if (saveBitmap != null) {
            imagePath = saveImageFile(extension, saveBitmap, fileLength, mContentLength);
        }

        if (filePath != null) {
            FileUtil.deleteFile(filePath);
        }

        return excuteBitmap(downloadBitmap, imagePath, extension);
    }

    private HttpResponse excuteBitmap(Bitmap downloadBitmap, String imagePath, String extension) {

        if (downloadBitmap != null && !downloadBitmap.isRecycled()) {
            if (isNeedCache) {
//				BitmapManager.getInstance().cacheNetworkBitmap(this.getBitmapCacheUrl(), downloadBitmap);
            }

            return new HttpBitmapResponse(HttpResponse.STATE_SUCCESS, this, downloadBitmap, imagePath);
        }
        return new HttpBitmapResponse(HttpResponse.STATE_HTTP_FALSE, this, null, null);
    }

    private String saveImageFile(String extension, Bitmap saveBitmap, long fileLength, long contentLength) {
        String imagePath = null;
        CompressFormat format = CompressFormat.PNG;

        if (BitmapUtils.EXTENSION_IMG_JPEG.equalsIgnoreCase(extension)) {
            format = CompressFormat.JPEG;
        } else {
            format = CompressFormat.PNG;
        }

        if (!Tools.isEmpty(getSavePath())) {
            imagePath = FileUtil.saveBitmap(getSavePath(), saveBitmap, format, extension);

            if (!Tools.isEmpty(imagePath)) {
                DownloadFileSaveUtil.saveFile(mContext, mCacheXmlUrl,
                        new DownloadFileSaveUtil.DownloadFileSave(imagePath));
            }

        }
        return imagePath;
    }

    public void setMaxMeasureSize(int maxMeasureWidth, int maxMeasureHeight) {
        mMaxMeasureWidth = maxMeasureWidth;
        mMaxMeasureHeight = maxMeasureHeight;
    }

    @Override
    public HttpResponse createErrorResponse(int state, HttpRequest httpTask) {
        return new HttpBitmapResponse(state, httpTask);
    }

    public void changeIsCache(boolean isCache) {
        this.isNeedCache = isCache;
    }

    @Override
    public boolean isComplete() {

        if (Tools.isEmpty(getFileName())) {
            return false;
        }
        return true;
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof HttpBitmapRequest) {
            HttpBitmapRequest tempRequest = (HttpBitmapRequest) o;
            String curdownloadUrl = getFileName() + getStyleName();
            String equalsDownloadUrl = tempRequest.getFileName() + tempRequest.getStyleName();
            if (!Tools.equals(curdownloadUrl, equalsDownloadUrl)) {
                return false;
            }
        }
        return super.equals(o);
    }

    public String getBitmapCacheUrl() {
        return mCacheXmlUrl + mMaxMeasureWidth + mMaxMeasureHeight;
    }

}
